package com.atguigu.gmall.product.init;

import com.atguigu.gmall.product.pojo.SkuInfo;
import com.atguigu.gmall.product.service.SkuInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 项目启动就要做的任务。
 *
 */

@Component
@Slf4j
public class InitRunner implements CommandLineRunner {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    SkuInfoService skuInfoService;
    
    public void initBitMap(){
        log.info("skuids:bitmap:"+ "进行初始化");
        List<SkuInfo> skuInfos = skuInfoService.lambdaQuery().select(SkuInfo::getId).list();
        skuInfos.stream().forEach(skuInfo -> {
            redisTemplate.opsForValue().setBit("skuids:bitmap:",skuInfo.getId(),true);
        });
        log.info("skuids:bitmap:" + "进行初始化完成");
    }

    @Override
    public void run(String... args) throws Exception {
        log.info("initRunner启动...");
        this.initBitMap();
        log.info("initBitMap初始化完成...");

    }
}
